<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\user\UserOccupant as UserOccupantModel;
use think\Validate;

/**
 * 入住人接口
 */
class Useroccupant extends Api
{
    //protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 列表
     *
     */
    public function lists()
    {
        $name = input('name','');
        $mobile = input('mobile','');
        $where = ['user_id'=>$this->auth->id];
        if ($name){
            $where['name'] = ['like', '%' . $name . '%'];
        }
        if ($mobile){
            $where['mobile'] = ['like', '%' . $mobile . '%'];
        }
        $list = UserOccupantModel::where($where)->select();

        $this->success('请求成功',$list);
    }

    //添加
    public function add()
    {
        $account_model = new UserOccupantModel();

        $data = [
            'user_id'=>$this->auth->id,
            'name'=>input('name',''),
            'id_card_num'=>input('id_card_num',''),
            'mobile'=>input('mobile',''),
        ];
        if (empty($data['name']))$this->error('请输入姓名');
        if (empty($data['id_card_num']))$this->error('请输入身份证号');
        if (empty($data['mobile']))$this->error('请输入手机号');
        if (!Validate::regex($data['mobile'], "^1\d{10}$")) {
            $this->error('手机号格式错误');
        }
        $length = strlen($data['id_card_num']);

        if ($length != 18){
            $this->error('请输入正确身份证号码');
        }
        $res = $account_model->save($data);

        $this->success('请求成功',$res);
    }

    //入住人信息
    public function info()
    {
        $id = input('id',0);

        $info = UserOccupantModel::get($id);

        if (!$info)$this->error('入住人不存在');

        $this->success('请求成功',$info);
    }

    //删除
    public function del()
    {
        $id = input('id',0);

        $info = UserOccupantModel::get($id);

        if (!$info)$this->error('不存在');

        $res = $info->delete();

        if ($res){
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //编辑
    public function edit()
    {
        $id = input('id',0);

        $account_model = new UserOccupantModel();

        $data = [
            'name'=>input('name',''),
            'id_card_num'=>input('id_card_num',''),
            'mobile'=>input('mobile',''),
        ];
        if (empty($data['name']))$this->error('请输入姓名');
        if (empty($data['id_card_num']))$this->error('请输入身份证号');
        if (empty($data['mobile']))$this->error('请输入手机号');
        if (!Validate::regex($data['mobile'], "^1\d{10}$")) {
            $this->error('手机号格式错误');
        }
        $length = strlen($data['id_card_num']);

        if ($length != 18){
            $this->error('请输入正确身份证号码');
        }
        $res = $account_model->save($data,['id'=>$id]);

        $this->success('编辑成功');
    }


}
